package com.heima.behavior.service.Impl;

import com.heima.behavior.interceptor.TokenInterceptor;
import com.heima.behavior.service.BehaviorService;
import com.heima.common.cache.CacheService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.heima.behavior.interceptor.TokenInterceptor.THREADLOCAL;

@Service
public class BehaviorServiceImpl implements BehaviorService {
    @Autowired
    CacheService cacheService;
    /**
     * 添加或取消点赞
     * @param likesBehaviorDto
     * @return
     */
    @Override
    public ResponseResult addOrDelete(LikesBehaviorDto likesBehaviorDto) {
        //文章id
        String articleId=likesBehaviorDto.getArticleId();
        //类型 0文章  1动态   2评论
        String type = String.valueOf(likesBehaviorDto.getType());
        //操作 0 点赞   1 取消
        short operation = likesBehaviorDto.getOperation();
        //获取用户id
        String userId = THREADLOCAL.get();

        switch (operation){
            case (short)1:
                cacheService.hDelete(articleId,"likeBehavior"+userId);
                break;
            case (short)0:
                //判断是否已经存在相应的key,不存在则创建并设置初始值为type
                if(!cacheService.hExists(articleId, "likeBehavior"+userId)){
                    cacheService.hPut(articleId,"likeBehavior"+userId,type);
                    return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
                }else {
                    return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"已经点赞过了");
                }
        }


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 阅读行为记录
     * @param readBehaviorDto
     * @return
     */
    @Override
    public ResponseResult readBehavior(ReadBehaviorDto readBehaviorDto) {
        //获取用户id
        String userId = THREADLOCAL.get();
        //阅读数量
        Integer count = readBehaviorDto.getCount();
        //文章id
        String articleId = readBehaviorDto.getArticleId();
        //判断是否有相应的key,没有先创建赋初始值1
        if(!cacheService.hExists(articleId,"readBehavior"+userId)){
            cacheService.hPut(articleId,"readBehavior"+userId,"1");
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //阅读数量增加
        cacheService.hIncrBy(articleId,"readBehavior"+userId,count);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 不喜欢行为
     * @param unLikesBehaviorDto
     * @return
     */
    @Override
    public ResponseResult unLikesBehavior(UnLikesBehaviorDto unLikesBehaviorDto) {
        //文章id
        String articleId = unLikesBehaviorDto.getArticleId();
        //0 不喜欢      1 取消不喜欢
        short type = unLikesBehaviorDto.getType();
        //获取用户id
        String userId = THREADLOCAL.get();
        switch (type){
            case (short)1:
                cacheService.hDelete(articleId,"unLikeBehavior"+userId);
                break;
            case (short)0:
                //判断是否已经存在相应的key,不存在则创建并设置初始值为1
                if(!cacheService.hExists(articleId, "unLikeBehavior"+userId)){
                    cacheService.hPut(articleId,"unLikeBehavior"+userId,"1");
                    return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
                }else {
                    return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"已经设置为不喜欢");
                }
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
